package com.fangmao.jetpack.fragment;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;

import com.fangmao.jetpack.viewmodel.HomeViewModel;
import com.fangmao.jetpack.R;
import com.fangmao.jetpack.databinding.FragmentHomeBinding;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.SavedStateViewModelFactory;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {


    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final HomeViewModel homeViewModel;
        homeViewModel = ViewModelProviders.of(this, new SavedStateViewModelFactory(getActivity().getApplication(), this)).get(HomeViewModel.class);
        FragmentHomeBinding homeBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false);

        homeBinding.setHomeData(homeViewModel);
        homeBinding.setLifecycleOwner(getActivity());

        homeBinding.seekBar.setProgress(homeViewModel.getNumber().getValue());
        homeBinding.seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                homeViewModel.addNumber(i);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        homeBinding.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NavController controller = Navigation.findNavController(view);
                controller.navigate(R.id.action_homeFragment_to_detailFragment);
            }
        });

        return homeBinding.getRoot();
//        return inflater.inflate(R.layout.fragment_home, container, false);
    }
}
